<?php



namespace App\Model;

use Illuminate\Database\Eloquent\Model;

/**
 *  App版本
 */
class AppVersion extends Model
{
    protected $table = 'app_version';

    const CACHE_KEY_LATEST = '';
    const PLATFORM = [
        0 => '全部',
        1 => 'IOS',
        2 => '安卓'
    ];

    const UPDATE_GROUP = [
        0 => '不限',
        UserProfile::ROLE_USER => '普通用户',
        UserProfile::ROLE_HOST => '认证用户'
    ];

    const UPDATE_GENDER = [
        -1 => '不限',
        1 => '男',
        0 => '女'
    ];

    const APPID = [
        100 => '约遇',
        200 => '缘友',
        300 => '聊友',
        400 => '同城交友'
    ];

    public function setChannelAttribute($channel)
    {
        $this->attributes['channel'] = implode(',', $channel);
    }

    /**
     * 获取最新的版本信息
     * @param int $platform
     * @return mixed
     */
    static function getLatest($platform = 1)
    {
        $where = [
            ['flag', '=', 1],
            ['valid_at', '<=', date('Y-m-d H:i:s')]
        ];
        $version = self::where($where)->whereIn('platform', [0, $platform])->orderBy('new_version', 'desc')->first();
        return $version ?: null;
    }

    static function getAllVersions()
    {
        $versions = self::orderBy('new_version', 'desc')->pluck('new_version')->toArray();
        $data = array_unique($versions);
        $ret = [];
        foreach ($data as $item) $ret[$item] = $item;
        return $ret;
    }

    /**
     * 当前ios在审核版本
     * @return string
     */
    static function iosChecking()
    {
        $version = self::getLatest();
        return $version ? $version->checking_version : '';
    }

    /**
     * 屏蔽地址
     * @return string[]
     */
    static function hideCity()
    {
        $city = [
            '全部','深圳市','合肥市','成都市','东莞市'
        ];
        return $city;
    }
}
